﻿package Welsy4.Connect.commands
{
	import com.adobe.cairngorm.commands.Command;
	import com.adobe.cairngorm.commands.SequenceCommand;
	import com.adobe.cairngorm.control.CairngormEvent;
	import com.adobe.cairngorm.control.CairngormEventDispatcher;
	import flash.net.URLVariables;
	
	
	import mx.rpc.IResponder;
	import mx.controls.Alert;
	
	import Welsy4.Connect.model.ConnectModel;
	import Welsy4.Connect.events.ChainEvent;
	import Welsy4.Connect.events.ResponseEvent;
	import Welsy4.Connect.business.CommonInfoDelegate;
	import Welsy4.Connect.vo.HttpServiceHandler;
	import Welsy4.Connect.vo.SCOShortcut;
	
	
	public class SCOShortcutCommand extends SequenceCommand implements Command, IResponder
	{
		
		private var eventURLVariable:URLVariables;
		
		override public function execute(event:CairngormEvent):void {
			
			ConnectModel.getInstance().addQueue("SCOShortcutCommand.execute");
			
			this.nextEvent = ChainEvent(event).nextChainedEvent;
			this.eventURLVariable = ChainEvent(event).eventRequest;
			
			var SCOshortcutDelegate:CommonInfoDelegate = new CommonInfoDelegate(this);
			
			SCOshortcutDelegate.getSCOShortcuts();
		}
		
		public function result(data:Object):void {
			
			ConnectModel.getInstance().addQueue("SCOShortcutCommand.result : TYPE = [" + eventURLVariable.type+"]");
			
			ConnectModel.getInstance().httpServiceHandler = new HttpServiceHandler();
			ConnectModel.getInstance().httpServiceHandler.setXML(data.result);
			
			var SCOShortcutRequested:SCOShortcut = ConnectModel.getInstance().httpServiceHandler.getSCOShortcutByType(eventURLVariable.type);
			
			
			
			if (SCOShortcutRequested != null) {
				ConnectModel.getInstance().SCOShortcutRequested = SCOShortcutRequested;
				//Alert.show("SCO-shortcut : " + SCOShortcutRequested.sco_id + ", " +  SCOShortcutRequested.domain_name + " \n\n" + ConnectModel.getInstance().httpServiceHandler.xml)
				this.executeNextCommand();
			} else {
				//Alert.show("No sco my-meeting found.");//\n Result : " + ConnectModel.getInstance().httpServiceHandler.xml);
				CairngormEventDispatcher.getInstance().dispatchEvent(new ResponseEvent( ResponseEvent.ERROR, false, ResponseEvent.NO_DATA, null, ResponseEvent.ERROR_DESCRIPTION_SCO_NOT_FOUND));
				
			}
			
			
		}
		
		public function fault(data:Object):void {
		    ConnectModel.getInstance().addQueue("SCOShortcutCommand.fault");	
			CairngormEventDispatcher.getInstance().dispatchEvent(new ResponseEvent( ResponseEvent.ERROR, false, ResponseEvent.SCO_SHORTCUT_COMMAND_FAULT, null, ResponseEvent.ERROR_DESCRIPTION_GENERIC_COMMAND_FAULT));
		}
		
	}
	
}